/*  
 * Copyright (C) 2011 Marta Spodymek
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 */
package org.prometheuscode.web.feedback.file;

import java.util.Map;

/**
 * This class is used for applying given strategy to the files.
 * 
 * TODO check if you can extend its functionality (aka more fine-grained)
 * 
 * @author marta
 * 
 */
public class FileHandler implements IFileHandler {

    private Map<String, IFileHandlerStrategy> fileStrategies;

    private IFileHandlerStrategy defaultStrategy = new FileSaverStrategy();

    private boolean useDefaultStrategy = true;

    public FileHandler() {
    }

    @Override
    public void handle(FileStrategyInfo info) {

	if (this.fileStrategies.containsKey(info.getType())) {
	    IFileHandlerStrategy handler = this.fileStrategies.get(info
		    .getType());
	    handler.process(info);
	} else if (this.useDefaultStrategy && (this.defaultStrategy != null)) {
	    this.defaultStrategy.process(info);
	} else {
	    throw new FileHandlerException(
		    "There is no IFileHandlerStrategy for: " + info.getType()
			    + " strategy.");
	}

    }

    public Map<String, IFileHandlerStrategy> getStrategies() {
	return this.fileStrategies;
    }

    public void setStrategies(Map<String, IFileHandlerStrategy> strategies) {
	this.fileStrategies = strategies;
    }

    public void setDefaultStrategy(IFileHandlerStrategy strategy) {
	this.defaultStrategy = strategy;
    }

    public IFileHandlerStrategy getDefaultStrategy() {
	return this.defaultStrategy;
    }

    public boolean isDefaultStrategyOn() {
	return this.useDefaultStrategy;
    }

    public void useDefaultStrategy(boolean use) {
	this.useDefaultStrategy = use;
    }

}
